package com.yanqu.road.server.cmd.estatewar.activity;

import com.yanqu.road.entity.decoration.DecorationSimple;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.estatewar.UserEstateWarData;
import com.yanqu.road.logic.pb.DecorationPb;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.decoration.DecorationProto;
import com.yanqu.road.pb.estatewar.EstateWarProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.DecorationModule;
import com.yanqu.road.server.gameplayer.module.player.EstateWarModule;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.palace.PalaceActivity;
import com.yanqu.road.server.manger.palace.PalaceActivityMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.List;

@Cmd(code = Protocol.S_PALACE_BATTLE, desc = "皇宫战斗")
public class PalaceBattleCmd extends AbstractCommand {
    private int activityId;
    private int pos;
    private long enemyUserId;
    private long enemyServerId;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        EstateWarProto.PalaceBattleReqMsg reqMsg = EstateWarProto.PalaceBattleReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        pos = reqMsg.getPos();
        enemyUserId = reqMsg.getEnemyUserId();
        enemyServerId = reqMsg.getEnemyServerId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        PalaceActivity palaceActivity = PalaceActivityMgr.getPalaceActivity(activityId);
        if(null == palaceActivity){
            ret = GameErrorCode.E_PALACE_ACTIVITY_NO_FOUND;
            return;
        }
        if(!SystemOpenMgr.systemOpen(player, eSystemId.PalaceActivity.getValue())){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        int estateType = 0;
        UserEstateWarData userEstateWarData = player.getModule(EstateWarModule.class).getUserEstateWarData();
        if(null != userEstateWarData){
            estateType = userEstateWarData.getType();
        }
        if(estateType < 6){
            ret = GameErrorCode.E_PALACE_CAN_NOT_BATTLE_ESTATE_FLOOR_NO_ENOUGH;
            return;
        }
        //发送到跨服
        EstateWarProto.PalaceBattleCrossReqMsg.Builder crossMsg = EstateWarProto.PalaceBattleCrossReqMsg.newBuilder();
        crossMsg.setActivityId(activityId);
        crossMsg.setPos(pos);
        crossMsg.setEnemyUserId(enemyUserId);
        crossMsg.setEnemyServerId(enemyServerId);
        crossMsg.setMyUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId())));
        crossMsg.setMyAbility(player.getModule(PatronsModule.class).getTotalPatronsAbility().toString());
        List<DecorationSimple> decorationSimpleList = player.getModule(DecorationModule.class).getHonoraryTitleDecorationSimpleList();
        for(DecorationSimple data : decorationSimpleList){
            DecorationProto.DecorationSimpleTempMsg.Builder msg = DecorationPb.parseDecorationSimpleTempMsg(data);
            if(null != msg){
                crossMsg.addDecorationList(msg);
            }
        }
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_PALACE_BATTLE, crossMsg);
        GamePlayerMgr.sendPacket(player.getUserId(), pbMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(0 != ret) {
            EstateWarProto.PalaceBattleRespMsg.Builder respMsg = EstateWarProto.PalaceBattleRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_PALACE_BATTLE, respMsg);
        }
    }
}
